package com.playcrab.adouban;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gdata.client.douban.DoubanService;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.douban.MiniblogEntry;
import com.google.gdata.util.ServiceException;
import com.playcrab.adouban.util.Constants;

public class ComposeMiniblog extends BaseActivity {
	private static final String LOG_TAG = "ComposeMiniblog";

	private static final int MENU_SCAN = Menu.FIRST + 1;
	private static final int MENU_ABOUT = Menu.FIRST + 10;
	private static final int MENU_EXIT = Menu.FIRST + 11;
	private Handler handler = new Handler();

	private static final String PACKAGE_NAME = "com.playcrab.adouban";

	/** Called when the activity is first created. */

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		
		setContentView(R.layout.compose_miniblog);
		checkLoggedin();
		
		Intent intent = getIntent();
		final EditText editText = (EditText) findViewById(R.id.compose_miniblog_edit);
        String action = intent.getAction();
        if (Intent.ACTION_SEND.equals(action) || Intent.ACTION_SENDTO.equals(action))
        {
        	  String text = intent.getStringExtra(Intent.EXTRA_TEXT);
        	  editText.setText(text);
        } 
		 
        final Button button = (Button) findViewById(R.id.compose_miniblog_button);
		OnClickListener listener = new OnClickListener() {
			public void onClick(View v) { 
			//	button.setText(ComposeMiniblog.this.getResources().getText(R.string.sending ) ); 
				button.refreshDrawableState();

				final DoubanService myService = getAuthedDoubanService();
				if(myService == null){
					return;
				}
				
			//	final ProgressDialog progressDialog = ProgressDialog.show(ComposeMiniblog.this, ComposeMiniblog.this.getResources().getText(R.string.waiting) ,
				//				ComposeMiniblog.this.getResources().getText(R.string.sending) , true);
				final Toast t = Toast.makeText(getApplicationContext(), 
						ComposeMiniblog.this.getResources().getText(R.string.compose_miniblog_failed),
						Toast.LENGTH_SHORT);
					new Thread() {
						public void run() { 
							handler.post(new Runnable() {
								public void run() {
									MiniblogEntry entry;
									try {
																				entry = myService
												.createSaying(new PlainTextConstruct((editText
														.getText()).toString()));
										if(entry != null){
											t.setText(ComposeMiniblog.this.getResources().getText(R.string.compose_miniblog_sent)); 
											Intent i = new Intent(ComposeMiniblog.this, Main.class);
											startActivity(i);
										}
									} catch (IOException e) { 
										e.printStackTrace(); 
										ComposeMiniblog.this.showInfo(getString(R.string.network_error));
									} catch (ServiceException e) {
										e.printStackTrace();
										ComposeMiniblog.this.showInfo(getString(R.string.service_error));
									} finally {
										t.show();
									//	button.setText(ComposeMiniblog.this.getResources().getText(R.string.compose_miniblog_button_text ) );
									
										//	progressDialog.dismiss();
									}
								}
							} );
						}
					}.start();
					
				
			}
		};
		
		
		button.setOnClickListener(listener);

	}

	public void onStart() {
		super.onStart();
	}

	public void onStop() {
		super.onStop();
	}
}